package gameSite;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       

    public CartServlet() 
    {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		CurrentUserSession currentUserSession = (CurrentUserSession)request.getAttribute("user");
		Cart cart = currentUserSession.getCurrentUserCart();
		ProductTableGateway productTableGateway = new ProductTableGateway();
		RecordSet rs;
		try 
		{
			rs = productTableGateway.getGamesByID((Integer)request.getAttribute("productID"));
			
			Product product = null;

			
			if(rs.getRowCount() ==1)
			{
				int productID = (Integer) rs.getValueAt(0, 0);
				String title =(String) rs.getValueAt(0, 1);
				String console = (String) rs.getValueAt(0, 2);
				String category = (String) rs.getValueAt(0, 3);
				String detail =(String)rs.getValueAt(0, 4);
				int price = (Integer)rs.getValueAt(0, 5);
				
				product = new Product(productID, title, console, category, detail, price);
			}
			
			cart.addProductToCart(product);
			
			request.getSession().setAttribute("user", currentUserSession);
			response.sendRedirect("CartJSP.jsp");
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
		

	}

}
